package com.zwcloud.zw.railway.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwcloud.zw.railway.entity.TrainInfo;
import com.zwcloud.zw.railway.mapper.CcxxallMapper;
import com.zwcloud.zw.railway.mapper.TrainInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Repository
public class TrainRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private CcxxallMapper ccxxallMapper;

    // 10天前
    private String getStartDateOfLast10Days() {
        return LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }

    // 两个月前
    private String getStartDateOfLastTwoMonths() {
        return LocalDate.now().minusMonths(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }

    // 今天
    private String getTodayDate() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }

    // 今天 + 昨天 平台名称
    public List<String> getNewRailwayPlatformNames(String yesterday) {
        String startDate = getStartDateOfLast10Days();
        String endDate = getTodayDate();

        String sql = "SELECT cpxx_chedaozh FROM cpxxall WHERE cpxx_chedate BETWEEN ? AND ?";
        return jdbcTemplate.queryForList(sql, String.class, startDate, endDate);
    }

    // 最近两个月 平台名称
    public List<String> getNewRailwayPlatformNamesTwoMonths() {
        String startDate = getStartDateOfLastTwoMonths();
        String endDate = getTodayDate();

        String sql = "SELECT cpxx_chedaozh FROM cpxxall WHERE cpxx_chedate BETWEEN ? AND ?";
        return jdbcTemplate.queryForList(sql, String.class, startDate, endDate);
    }

    // 今天+昨天 ccxx数据
    public List<TrainInfo> getCcxxTrainInfoData() {
        String startDate = getStartDateOfLast10Days();
        String endDate = getTodayDate();

        String sql = "SELECT p.* " +
                "FROM ccxxall c " +
                "JOIN cpxxall p ON c.ccxx_pici = p.cpxx_pici " +
                "WHERE c.ccxx_dateend BETWEEN ? AND ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TrainInfo.class), startDate, endDate);
    }

    // 最近两个月 ccxx数据
    public List<TrainInfo> getCcxxTrainInfoDataTwoMonths() {
        String startDate = getStartDateOfLastTwoMonths();
        String endDate = getTodayDate();

        String sql = "SELECT p.* " +
                "FROM ccxxall c " +
                "JOIN cpxxall p ON c.ccxx_pici = p.cpxx_pici " +
                "WHERE c.ccxx_dateend BETWEEN ? AND ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TrainInfo.class), startDate, endDate);
    }

    // cpxx_key 列表 → 无时间限制
    public List<Long> getCpxxKeysByRailwayPlatformName(String railwayPlatformName) {
        QueryWrapper<TrainInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cpxx_chedaozh", railwayPlatformName);
        List<TrainInfo> trainInfoList = trainInfoMapper.selectList(queryWrapper);
        List<Long> cpxxKeys = new ArrayList<>();
        if (trainInfoList != null && !trainInfoList.isEmpty()) {
            for (TrainInfo trainInfo : trainInfoList) {
                String cpxxChehao = String.valueOf(trainInfo.getCpxxChehao());
                if (cpxxChehao != null && !cpxxChehao.isEmpty()) {
                    String cleanedChehao = cpxxChehao.replaceAll("[^0-9]", "");
                    if (!cleanedChehao.isEmpty()) {
                        try {
                            Long.parseLong(cleanedChehao);
                            cpxxKeys.add(trainInfo.getCpxxKey().longValue());
                        } catch (NumberFormatException e) {
                            System.err.println("Invalid data found: " + cpxxChehao);
                        }
                    }
                }
            }
        }
        return cpxxKeys;
    }
}
